package com.example.controller;

import com.example.result.Result;
import com.example.pojo.User;
import com.example.result.ResultCode;
import com.example.service.UserService;
import com.example.utils.JwtUtil;
import com.example.utils.Md5Util;
import com.example.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
@Validated
public class UserController{

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(String username, @Pattern(regexp = "^\\S{5,16}$") String password){
        if (!userService.isUserExist(username)) {
            userService.addNewUser(username,password);
            return Result.success(200,"账号注册成功！");
        }else {
            return Result.error(400,"用户名已存在！");
        }
    }

    @PostMapping("/login")
    public Result login(String username, @Pattern(regexp = "^\\S{5,16}$") String password){
        User user = userService.findByUserName(username);
        if (user == null) {
            return Result.error(400,"账号或密码错误");
        }
        if (!Md5Util.getMD5String(password).equals(user.getPassword())) {
            return Result.error(400,"账号或密码错误");
        }
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",user.getId());
        claims.put("username",user.getUsername());
        String token = JwtUtil.genToken(claims);
        return Result.success(200,"登陆成功！", token);
    }

    @GetMapping("/userInfo")
    public Result userInfo(){
        Map<String,Object> claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUserName(username);
        return Result.success(200,"success",user);
    }

    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody @Validated User user){
        if (userService.updateUserInfo(user)) {
            return Result.success(200, "修改成功");
        } else return Result.error(400,"修改失败");
    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam @URL String url){
        userService.updateAvatar(url);
        return Result.success(200,"success");
    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> params){
        String oldPwd = params.get("oldPwd");
        String newPwd = params.get("newPwd");
        String rePwd = params.get("rePwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error(400,"缺少必要参数");
        }

        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        String password = userService.findByUserName(username).getPassword();
        if (!Md5Util.getMD5String(oldPwd).equals(password)) {
            return Result.error(400,"密码错误");
        }

        if (!newPwd.equals(rePwd)) {
            return Result.error(400,"新密码输入不相同");
        }

        if (newPwd.equals(oldPwd)){
            return Result.error(400,"新密码不能与旧密码相同");
        }

        userService.updatePwd(newPwd);
        return Result.success(200,"密码修改成功");
    }
}